package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.YxStoreBrand;
import co.yixiang.modules.shop.repository.YxStoreBrandRepository;
import co.yixiang.modules.shop.service.YxStoreBrandService;
import co.yixiang.modules.shop.service.dto.YxStoreBrandDto;
import co.yixiang.modules.shop.service.dto.YxStoreBrandQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxStoreBrandMapper;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author frq
* @date 2020-07-10
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreBrandServiceImpl implements YxStoreBrandService {

    private final YxStoreBrandRepository yxStoreBrandRepository;

    private final YxStoreBrandMapper yxStoreBrandMapper;

    public YxStoreBrandServiceImpl(YxStoreBrandRepository yxStoreBrandRepository, YxStoreBrandMapper yxStoreBrandMapper) {
        this.yxStoreBrandRepository = yxStoreBrandRepository;
        this.yxStoreBrandMapper = yxStoreBrandMapper;
    }

    @Override
    public Map<String,Object> queryAll(YxStoreBrandQueryCriteria criteria, Pageable pageable){
        Page<YxStoreBrand> page = yxStoreBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStoreBrandMapper::toDto));
    }

    @Override
    public List<YxStoreBrandDto> queryAll(YxStoreBrandQueryCriteria criteria){
        return yxStoreBrandMapper.toDto(yxStoreBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public YxStoreBrandDto findById(Integer id) {
        YxStoreBrand yxStoreBrand = yxStoreBrandRepository.findById(id).orElseGet(YxStoreBrand::new);
        ValidationUtil.isNull(yxStoreBrand.getId(),"YxStoreBrand","id",id);
        return yxStoreBrandMapper.toDto(yxStoreBrand);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxStoreBrandDto create(YxStoreBrand resources) {
        resources.setAddTime(new Date());
        resources.setIsDel(0);
        return yxStoreBrandMapper.toDto(yxStoreBrandRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreBrand resources) {
        YxStoreBrand yxStoreBrand = yxStoreBrandRepository.findById(resources.getId()).orElseGet(YxStoreBrand::new);
        ValidationUtil.isNull( yxStoreBrand.getId(),"YxStoreBrand","id",resources.getId());
        yxStoreBrand.copy(resources);
        yxStoreBrandRepository.save(yxStoreBrand);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreBrandRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YxStoreBrandDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreBrandDto yxStoreBrand : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("品牌名称", yxStoreBrand.getBrandName());
            map.put("排序", yxStoreBrand.getSort());
            map.put("图标", yxStoreBrand.getPic());
            map.put("是否推荐", yxStoreBrand.getIsShow());
            map.put("添加时间", yxStoreBrand.getAddTime());
            map.put("删除状态", yxStoreBrand.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onSale(Integer id, int isShow) {
        if (isShow == 1) {
            isShow = 0;
        } else {
            isShow = 1;
        }
        yxStoreBrandRepository.updateOnsale(isShow, id);
    }


}